package com.oneday.ashop.console.authority;

import com.oneday.ashop.console.base.shiro.MyRealm;
import com.oneday.ashop.console.resource.ResourceService;
import com.oneday.ashop.core.base.util.CollectionUtil;
import com.oneday.ashop.core.entity.Admin;
import com.oneday.ashop.core.entity.Page;
import com.oneday.ashop.core.entity.Resource;
import com.oneday.ashop.core.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限管理的控制层
 * Created by admin on 2016/7/3.
 */
@Controller
@RequestMapping("auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private MyRealm myRealm;

    /**
     * 获得登陆页面
     * @return
     */
    @RequestMapping("get_login_page")
    public String getLoginPage(HttpServletRequest request) {
        System.out.println(request.getParameter("desc"));
        return "login";
    }

    /**
     * 主页面
     * @param model
     * @return
     */
    @RequestMapping("home_page")
    public String homePage(Model model) {
        return "home";
    }

    /**
     * 资源管理
     * @param model
     * @return
     */
    @RequestMapping("resource_manager")
    public String resourceManager(Model model) {
        model.addAttribute("resources", authService.getALlResources());
        return "auth/resource_manager";
    }

    /**
     * 角色管理
     * @param model
     * @return
     */
    @RequestMapping("role_manager")
    public String roleManager(Model model) {
        List<Role> roles = authService.roleManager();
        model.addAttribute("roles", roles);
        return "auth/role_manager";
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("role_add")
    public void roleAdd(Role role) {
        authService.roleAdd(role);
    }

    /**
     * 查抄资源分页数据
     * @param model
     */
    @RequestMapping("get_resource_page")
    public void getResourcePage(Page<Resource> page, Model model) {
        model.addAttribute("resources", authService.getALlResources());
    }

    /**
     * 添加资源
     */
    @RequestMapping("add_resource")
    public void addResource(Resource resource) {
        authService.addResource(resource);
    }

    /**
     * 删除资源
     * @param resourceId
     */
    @RequestMapping("delete_resource")
    public void deleteResource(Integer resourceId) {
        authService.deleteResource(resourceId);
    }

    /**
     * 获得ztree格式的目录数据
     */
    @RequestMapping("get_z_tree_resource")
    public void getZTreeResource(@RequestParam(required = true)Integer roleId, Model model) {
        Object[] result = authService.getZTreeResource(roleId);
        model.addAttribute("pageResources", result[0]);
        model.addAttribute("buttonResources", result[1]);
        model.addAttribute("jsonResources", result[2]);
    }


    /**
     * 修改角色的资源
     * @param roleId
     * @param resourceIds
     */
    @RequestMapping("modify_resource_of_role")
    public void modifyResourceOfRole(Integer roleId, String resourceIds) {
        authService.modifyResourceOfRole(roleId, resourceIds);
        //清除所有的用户的授权信息
        myRealm.clearAllCachedAuthorizationInfo();
    }

    /**
     * 管理员管理
     * @param page
     */
    @RequestMapping("admin_manager")
    public String adminManager(Page<Admin> page) {
        authService.adminManager(page);
        return "auth/admin_manager";
    }

    /**
     * 添加管理员
     * @param admin
     */
    @RequestMapping("add_admin")
    public void addAdmin(Admin admin) {
        authService.addAdmin(admin);
    }

    /**
     * 获得所有的角色
     * @param model
     */
    @RequestMapping("get_all_roles")
    public void getAllRoles(Integer adminId, Model model) {
        model.addAttribute("roles", authService.getAdminRoles(adminId));
    }

    /**
     * 给用户分配角色
     * @param admin
     */
    @RequestMapping("assign_roles")
    public void assignRoles(@RequestParam(required = true) Integer adminId, Admin admin) {
        authService.assignRoles(adminId, admin.getRoleIds());
    }
}
